package com.situ.mall.controller;

import com.situ.common.util.Result;
import com.situ.mall.pojo.entity.Coupon;
import com.situ.mall.pojo.entity.User;
import com.situ.mall.service.ICouponService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/coupon")
public class CouponController {
    @Autowired
    private ICouponService couponService;

    @RequestMapping("/addCoupon")
    @ResponseBody
    public void addCoupon(HttpSession session, Coupon coupon) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        coupon.setUserId(userId);
        System.out.println(coupon);
        couponService.add(coupon);
    }
    @RequestMapping("/selectByUserId")
    @ResponseBody
    public Result selectByUserId(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        List<Coupon> list = couponService.selectByUserId(userId);
        return Result.ok(list);
    }
    @RequestMapping("/selectById")
    @ResponseBody
    public Result selectById(Integer id) {
        Coupon coupon = couponService.selectById(id);
        return Result.ok(coupon);
    }
    @RequestMapping("/couponListSelectByUserId")
    @ResponseBody
    public Result couponListSelectByUserId(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        List<Coupon> list = couponService.couponListSelectByUserId(userId);
        return Result.ok(list);
    }
    @RequestMapping("/selectByUserIdNowBuy")
    @ResponseBody
    public Result selectByUserIdNowBuy(HttpSession session, Integer productId, Integer quantity) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        List<Coupon> list = couponService.selectByUserIdNowBuy(userId, productId, quantity);
        return Result.ok(list);
    }
}
